package com.smart.community.region.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smart.community.commons.utils.SecurityUtils;
import com.smart.community.region.entity.Building;
import com.smart.community.region.entity.Household;
import com.smart.community.region.entity.Unit;
import com.smart.community.region.mapper.BuildingMapper;
import com.smart.community.region.mapper.HouseholdMapper;
import com.smart.community.region.mapper.UnitMapper;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;

/**
 * 社区数据导入服务
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */
@Slf4j
@Service
public class CommunityDataImportService {

    @Autowired
    private BuildingMapper buildingMapper;
    
    @Autowired
    private UnitMapper unitMapper;
    
    @Autowired
    private HouseholdMapper householdMapper;

    /**
     * 批量导入楼栋数据
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public int importBuildings(List<Map<String, Object>> buildingData, Long communityId) {
        log.info("开始导入楼栋数据，社区ID：{}，数据量：{}", communityId, buildingData.size());
        
        List<Building> buildings = new ArrayList<>();
        for (Map<String, Object> data : buildingData) {
            Building building = convertToBuilding(data, communityId);
            buildings.add(building);
        }
        
        int successCount = 0;
        for (Building building : buildings) {
            try {
                buildingMapper.insert(building);
                successCount++;
                log.debug("楼栋导入成功：{}", building.getBuildingNo());
            } catch (Exception e) {
                log.error("楼栋导入失败：{}，错误：{}", building.getBuildingNo(), e.getMessage());
                throw new RuntimeException("楼栋导入失败：" + building.getBuildingNo() + "，" + e.getMessage());
            }
        }
        
        log.info("楼栋数据导入完成，成功：{}，总数：{}", successCount, buildingData.size());
        return successCount;
    }

    /**
     * 批量导入单元数据
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public int importUnits(List<Map<String, Object>> unitData, Long communityId, Map<String, Long> buildingIdMap) {
        log.info("开始导入单元数据，社区ID：{}，数据量：{}", communityId, unitData.size());
        
        List<Unit> units = new ArrayList<>();
        for (Map<String, Object> data : unitData) {
            Unit unit = convertToUnit(data, communityId, buildingIdMap);
            units.add(unit);
        }
        
        int successCount = 0;
        for (Unit unit : units) {
            try {
                unitMapper.insert(unit);
                successCount++;
                log.debug("单元导入成功：{}", unit.getUnitName());
            } catch (Exception e) {
                log.error("单元导入失败：{}，错误：{}", unit.getUnitName(), e.getMessage());
                throw new RuntimeException("单元导入失败：" + unit.getUnitName() + "，" + e.getMessage());
            }
        }
        
        log.info("单元数据导入完成，成功：{}，总数：{}", successCount, unitData.size());
        return successCount;
    }

    /**
     * 批量导入房户数据
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public int importHouseholds(List<Map<String, Object>> householdData, Long communityId, 
                               Map<String, Long> buildingIdMap, Map<String, Long> unitIdMap) {
        log.info("开始导入房户数据，社区ID：{}，数据量：{}", communityId, householdData.size());
        
        List<Household> households = new ArrayList<>();
        for (Map<String, Object> data : householdData) {
            Household household = convertToHousehold(data, communityId, buildingIdMap, unitIdMap);
            households.add(household);
        }
        
        int successCount = 0;
        for (Household household : households) {
            try {
                householdMapper.insert(household);
                successCount++;
                log.debug("房户导入成功：{}", household.getHouseholdCode());
            } catch (Exception e) {
                log.error("房户导入失败：{}，错误：{}", household.getHouseholdCode(), e.getMessage());
                throw new RuntimeException("房户导入失败：" + household.getHouseholdCode() + "，" + e.getMessage());
            }
        }
        
        log.info("房户数据导入完成，成功：{}，总数：{}", successCount, householdData.size());
        return successCount;
    }

    /**
     * 转换楼栋数据
     */
    private Building convertToBuilding(Map<String, Object> data, Long communityId) {
        Building building = new Building();
        
        building.setBuildingNo(getStringValue(data, "buildingNo"));
        building.setBuildingName(getStringValue(data, "buildingName"));
        building.setCommunityId(communityId);
        building.setBuildingType(getStringValue(data, "buildingType"));
        building.setFloorCount(getIntegerValue(data, "floorCount"));
        building.setUnitCount(getIntegerValue(data, "unitCount"));
        building.setHouseholdCount(0); // 初始为0
        building.setUnitsPerFloor(getIntegerValue(data, "unitsPerFloor"));
        building.setBuildYear(getIntegerValue(data, "buildYear"));
        building.setRemark(getStringValue(data, "remark"));
        
        // 设置默认值
        building.setStatus(1); // 启用状态
        building.setCreateBy(SecurityUtils.getCurrentUserId());
        building.setCreateTime(LocalDateTime.now());
        building.setUpdateBy(SecurityUtils.getCurrentUserId());
        building.setUpdateTime(LocalDateTime.now());
        
        return building;
    }

    /**
     * 转换单元数据
     */
    private Unit convertToUnit(Map<String, Object> data, Long communityId, Map<String, Long> buildingIdMap) {
        Unit unit = new Unit();
        
        String buildingNo = getStringValue(data, "buildingNo");
        Long buildingId = buildingIdMap.get(buildingNo);
        if (buildingId == null) {
            throw new RuntimeException("楼栋不存在：" + buildingNo);
        }
        
        unit.setUnitName(getStringValue(data, "unitName"));
        unit.setUnitNo(getStringValue(data, "unitNo"));
        unit.setCommunityId(communityId);
        unit.setBuildingId(buildingId);
        unit.setFloorCount(getIntegerValue(data, "floorCount"));
        unit.setRoomCount(getIntegerValue(data, "roomCount"));
        unit.setHouseholdCount(getIntegerValue(data, "householdCount"));
        unit.setHasElevator("是".equals(getStringValue(data, "hasElevator")) ? 1 : 0);
        unit.setElevatorCount(getIntegerValue(data, "elevatorCount"));
        unit.setDoorAccessType(getDoorAccessType(getStringValue(data, "doorAccessType")));
        unit.setRemark(getStringValue(data, "remark"));
        
        // 设置默认值
        unit.setStatus(1); // 正常使用
        unit.setCreateBy(SecurityUtils.getCurrentUserId());
        unit.setCreateTime(LocalDateTime.now());
        unit.setUpdateBy(SecurityUtils.getCurrentUserId());
        unit.setUpdateTime(LocalDateTime.now());
        
        return unit;
    }

    /**
     * 转换房户数据
     */
    private Household convertToHousehold(Map<String, Object> data, Long communityId, 
                                        Map<String, Long> buildingIdMap, Map<String, Long> unitIdMap) {
        Household household = new Household();
        
        String buildingNo = getStringValue(data, "buildingNo");
        Integer unitNo = getIntegerValue(data, "unitNo");
        Long buildingId = buildingIdMap.get(buildingNo);
        Long unitId = unitIdMap.get(buildingNo + "-" + unitNo);
        
        if (buildingId == null) {
            throw new RuntimeException("楼栋不存在：" + buildingNo);
        }
        if (unitId == null) {
            throw new RuntimeException("单元不存在：楼栋" + buildingNo + "单元" + unitNo);
        }
        
        household.setHouseholdCode(getStringValue(data, "householdCode"));
        household.setCommunityId(communityId);
        household.setBuildingId(buildingId);
        household.setUnitId(unitId);
        household.setRoomNumber(getStringValue(data, "roomNumber"));
        household.setFloorNumber(getIntegerValue(data, "floorNumber"));
        household.setArea(getBigDecimalValue(data, "area"));
        household.setRoomType(getRoomType(getStringValue(data, "houseType")));
        household.setOrientation(getStringValue(data, "orientation"));
        household.setStatus(getHouseholdStatus(getStringValue(data, "status")));
        household.setDescription(getStringValue(data, "remark"));
        
        // 设置默认值
        household.setCreateBy(SecurityUtils.getCurrentUserId());
        household.setCreateTime(LocalDateTime.now());
        household.setUpdateBy(SecurityUtils.getCurrentUserId());
        household.setUpdateTime(LocalDateTime.now());
        
        return household;
    }

    /**
     * 获取门禁类型
     */
    private Integer getDoorAccessType(String type) {
        if (type == null) return 1; // 默认密码
        switch (type) {
            case "密码": return 1;
            case "刷卡": return 2;
            case "人脸识别": return 3;
            case "指纹": return 4;
            default: return 1;
        }
    }

    /**
     * 获取房间类型
     */
    private Integer getRoomType(String type) {
        if (type == null) return 2; // 默认两室
        switch (type) {
            case "一室": return 1;
            case "两室": return 2;
            case "三室": return 3;
            case "四室及以上": return 4;
            default: return 2;
        }
    }

    /**
     * 获取房户状态
     */
    private Integer getHouseholdStatus(String status) {
        if (status == null) return 0; // 默认空置
        switch (status) {
            case "空置": return 0;
            case "已售": return 1;
            case "已租": return 3;
            default: return 0;
        }
    }

    /**
     * 获取字符串值
     */
    private String getStringValue(Map<String, Object> data, String key) {
        Object value = data.get(key);
        return value != null ? value.toString().trim() : null;
    }

    /**
     * 获取整数值
     */
    private Integer getIntegerValue(Map<String, Object> data, String key) {
        Object value = data.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        try {
            return Integer.parseInt(value.toString().trim());
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 获取BigDecimal值
     */
    private BigDecimal getBigDecimalValue(Map<String, Object> data, String key) {
        Object value = data.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (value instanceof Number) {
            return new BigDecimal(value.toString());
        }
        try {
            return new BigDecimal(value.toString().trim());
        } catch (NumberFormatException e) {
            return null;
        }
    }
}
